package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import org.bson.types.ObjectId;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lxb哦吼吼
 * @version 1.0
 * @description: TODO
 * @date 2021/11/24 18:38
 */

@Service
public class MQService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendLog(Long userId, String type, String key, String busId) {
        try {
            Map map = new HashMap();
            map.put("userId", userId.toString());
            map.put("type", type);
            map.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            map.put("objId", busId);
            String message = JSON.toJSONString(map);
            amqpTemplate.convertAndSend("tanhua.log.exchange", "log." + key, message);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }

    public void sendAudi(Long userId, ObjectId movementId) {
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("userId", userId);
        map.put("movementId", movementId);
        try {
            amqpTemplate.convertAndSend("tanhua.audit.exchange", "audit.movement", map);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }
}
